<template>
  <div>
    <transition name="el-zoom-in-center">
      <div class="jsbos-preview-main">
        <div v-if="pageType == 'add'" class="jsbos-common-page-header">
          <el-page-header @back="goBack" content="新建流程"> </el-page-header>
          <div class="options">
            <el-button @click="goBack()">{{
              $t("common.cancelButton")
            }}</el-button>
          </div>
        </div>
        <div class="main">
          <el-tabs
            tab-position="left"
            style="height: 100%"
            v-model="category"
            class="flow-tabs"
          >
            <el-tab-pane label="全部流程" name=""></el-tab-pane>
            <el-tab-pane
              :label="item.fullName"
              :name="item.enCode"
              v-for="item in categoryListLast"
              :key="item.enCode"
            ></el-tab-pane>
            <div class="box">
              <el-row class="jsbos-common-search-box" :gutter="16">
                <el-form @submit.native.prevent>
                  <el-col :span="6">
                    <el-form-item label="关键词">
                      <el-input
                        v-model="keyword"
                        placeholder="请输入关键词查询"
                        clearable
                        @keyup.enter.native="search()"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item>
                      <el-button
                        type="primary"
                        icon="el-icon-search"
                        @click="search()"
                      >
                        {{ $t("common.search") }}</el-button
                      >
                      <el-button icon="el-icon-refresh-right" @click="reset()"
                        >{{ $t("common.reset") }}
                      </el-button>
                    </el-form-item>
                  </el-col>
                </el-form>
              </el-row>
              <div
                class="list"
                ref="infiniteBody"
                v-loading="listLoading && listQuery.currentPage == 1"
              >
                <el-row :gutter="20" v-if="list.length">
                  <el-col :span="24" v-for="(item2, j) in list" :key="j">
                    <h3 :id="item2.classId" style="padding-bottom: 20px; padding-left: 10px">
                      {{ item2.enCode }}
                    </h3>
                    <el-col
                      :span="6"
                      v-for="(item, i) in item2.data"
                      :key="i"
                      class="item"
                      @click.native="jump(item)"
                    >
                      <el-card shadow="hover">
                        <div
                          class="box-icon"
                          :style="{
                            backgroundColor: item.iconBackground || '#008cff',
                          }"
                        >
                          <i :class="item.icon"></i>
                        </div>
                        <span class="title">{{ item.fullName }}</span>
                      </el-card>
                    </el-col>
                  </el-col>
                </el-row>
                <el-empty
                  description="暂无数据"
                  :image-size="120"
                  v-else
                ></el-empty>
              </div>
            </div>
          </el-tabs>
        </div>
      </div>
    </transition>
    <FlowBox v-if="formVisible" ref="FlowBox" @close="closeForm" />
  </div>
</template>

<script>
import {
  FlowEnginePageList,
  FlowEnginePageListGroup,
} from "@/api/workFlow/FlowEngine";
import FlowBox from "../components/FlowBox";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";

import settings from "@/settings";
export default {
  name: "createdFlow",
  components: { FlowBox },
  data() {
    return {
      keyword: "",
      category: "",
      listQuery: {
        currentPage: 1,
        pageSize: 50,
        sort: "desc",
        sidx: "",
      },
      total: 0,
      finish: false,
      list: [],
      engineData: {},
      listLoading: true,
      categoryList: [],
      categoryListLast: [],
      formVisible: false,
      pageType: "",
      allList: []
    };
  },
  watch: {
    category(val) {
      // console.log(this.$route.query.type);
      if (!this.categoryList.length) {
        this.getDictionaryData();
      }
      setTimeout(() => {
        this.reset();
      }, 100);
    },
  },
  created() {
    // this.getDictionaryData();
  },
  methods: {
    goBack() {
      this.$emit("close");
    },
    init(type) {
      this.pageType = type ? type : "";
      // this.getDictionaryData();
      //页面触底滚动加载
      // this.$nextTick(() => {
      //   this.bindScroll();
      // });
      // this.initData();
    },
    reset() {
      this.keyword = "";
      this.search();
    },
    search() {
      this.list = [];
      this.finish = false;
      this.listQuery = {
        currentPage: 1,
        pageSize: 50,
        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
    bindScroll() {
      let _this = this,
        vBody = _this.$refs.infiniteBody;
      vBody.addEventListener("scroll", function () {
        if (
          vBody.scrollHeight - vBody.clientHeight - vBody.scrollTop <= 200 &&
          !_this.listLoading &&
          !_this.finish
        ) {
          _this.listQuery.currentPage += 1;
          _this.initData();
        }
      });
    },
    initData() {
      this.listLoading = true;
      let query = {
        ...this.listQuery,
        keyword: this.keyword,
        category: this.category == 0 ? "" : this.category,
      };
      FlowEnginePageListGroup(query).then((res) => {
        this.list = []
        this.list = [...this.list, ...res.data];
        this.list.forEach((element) => {
          if (this.categoryList) {
            this.categoryList.forEach((e) => {
              if (e.enCode === element.enCode) {
                element.classId = e.enCode
                element.enCode = e.fullName;
              }
            });
          }
        });
        let copyList = []
        this.categoryList.forEach((item,index) => {
          this.list.forEach(el => {
            if(item.fullName == el.enCode) {
              copyList.push(el)
            }
          })
        })
        this.list = copyList
        if(!this.allList.length) {
          this.allList = copyList
        }
        let categoryListCopy = []
        this.allList.forEach(o => {
          let isHave = this.categoryList.find(value => {
            return value.enCode == o.classId
          })
          if(isHave) {
            categoryListCopy.push(isHave)
          }
        })
        this.categoryListLast = categoryListCopy
        // this.total = res.data.pagination.total;
        this.listLoading = false;
      });
    },
    showCategoryName(enCode) {
      if (this.categoryList) {
        this.categoryList.forEach((element) => {
          if (element.enCode === enCode) {
            console.log("encode", enCode, "name;;;", element.fullName);
            return element.fullName;
          }
        });
      }
    },
    getDictionaryData() {
      // this.$store
      //   .dispatch("base/getDictionaryData", { encode: "WorkFlowCategory" })
      //   .then((res) => {
      //     this.categoryList = res;
      //   });
      getDictionaryDataSelector("507f4f5df86b47588138f321e0b0dac7").then(
        (res) => {
          this.categoryList = res.data.list;
        }
      );
    },
    jump(item) {
      console.log("222", this.pageType, item);
      if (!item.enCode) {
        this.$message({
          type: "error",
          message: "流程不存在",
        });
        return;
      }
      if (this.pageType != "add") {
        let data = {
          id: "",
          enCode: item.enCode,
          flowId: item.id,
          formType: item.formType,
          opType: "-1",
        };
        this.formVisible = true;
        this.$nextTick(() => {
          console.log(this.$refs.FlowBox);
          this.$refs.FlowBox.init(data);
          this.flowVisible = false;
        });
      } else {
        this.$emit("choiceFlow", item);
      }
    },
    closeForm(isRefresh) {
      this.formVisible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.main {
  height: 100%;
  display: flex;
  flex-direction: column;
  color: #606266;
  .flow-tabs {
    >>> .el-tabs__item {
      text-align: left !important;
      width: 160px !important;
    }
    >>> .el-tabs__content {
      height: 100%;

      .el-tab-pane {
        height: 0;
        display: none !important;
      }
    }
  }
  >>> .is-horizontal {
    display: none;
  }
  >>> .el-scrollbar__view {
    overflow: hidden;
  }
  .box {
    height: 100%;
    display: flex;
    flex-direction: column;
  }
  .list {
    flex: 1;
    margin-top: 10px;
    overflow: hidden auto;
    .item {
      margin-bottom: 20px;
      cursor: pointer;
      >>> .el-card__body {
        display: flex;
        align-items: center;
        padding: 15px;
      }
      .box-icon {
        width: 48px;
        height: 48px;
        border-radius: 10px;
        text-align: center;
        background-color: #ccc;
        display: inline-block;
        margin-right: 15px;
        i {
          text-align: center;
          font-size: 36px;
          color: #fff;
          line-height: 48px;
        }
      }
      .title {
        display: inline-block;
        width: calc(100% - 63px);
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        -webkit-box-orient: vertical;
        word-break: break-all;
        font-size: 14px;
      }
    }
  }
}
</style>
